﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint.Utilities;

namespace TSOL.FastSPWeb.SP2007.FeatureReceiver
{
    public class FastSPWebFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPUrlZone zone in webApp.IisSettings.Keys)
            {
                SPIisSettings oSettings = webApp.IisSettings[zone];
                string sourcePath = string.Format(@"{0}\FEATURES\{1}\", SPUtility.GetGenericSetupPath("Template"), @"\TSOL.FastSPWeb.2007.GlobalAsax");
                string destPath = oSettings.Path.ToString();
                if (!File.Exists(Path.Combine(sourcePath, "global.asax_FastSPWeb")))
                    File.Copy(Path.Combine(destPath, "global.asax"), Path.Combine(destPath, "global.asax_FastSPWeb"), true);

                File.Copy(Path.Combine(sourcePath, "global.asax"), Path.Combine(destPath, "global.asax"), true);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPUrlZone zone in webApp.IisSettings.Keys)
            {
                SPIisSettings oSettings = webApp.IisSettings[zone];
                string destPath = oSettings.Path.ToString();
                File.Copy(Path.Combine(destPath, "global.asax_SPWebThreadPool"), Path.Combine(destPath, "global.asax"), true);
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}